﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using BitPortal.Models;
using BitPortal.Utils;
namespace BitPortal.Business
{
    public static partial class ProductCategoryBLL
    {
        public static int Insert(this ProductCategory cat)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            bitEnt.ProductCategories.AddObject(cat);
            return bitEnt.SaveChanges();
        }

        public static void DeletePermanently(ProductCategory cat, ExtendedBitPortalEntities bitEnt)
        {
            foreach (ProductCategory child in bitEnt.ProductCategories.Where(ent => ent.ParentID == cat.CategoryID))
            {
                DeletePermanently(child, bitEnt);
            }
            bitEnt.ProductCategories.DeleteObject(cat);
        }

        /// <summary>
        /// Xoa truc tiep
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static void DeletePermanently(int categoryID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            ProductCategory category = bitEnt.ProductCategories.SingleOrDefault(ac => ac.CategoryID == categoryID);
            if (category != null)
            {
                DeletePermanently(category, bitEnt);
            }
            bitEnt.SaveChanges();
        }

        public static void Delete(ProductCategory cat, ExtendedBitPortalEntities bitEnt)
        {
            foreach (ProductCategory child in bitEnt.ProductCategories.Where(ent => ent.ParentID == cat.CategoryID))
            {
                Delete(child, bitEnt);
            }
            cat.IsDeleted = true;
        }
        /// <summary>
        /// Xoa tam thoi(dat IsDeleted = true)
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static void Delete(int categoryID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            ProductCategory category = bitEnt.ProductCategories.SingleOrDefault(ac => ac.CategoryID == categoryID);
            if (category != null)
            {
                Delete(category, bitEnt);
            }
            bitEnt.SaveChanges();
        }


        /// <summary>
        /// Lay ra danh sach tat ca nhom con chau cua nhom hien tai.
        /// </summary>
        /// <param name="parentcate"></param>
        /// <param name="childs"></param>
        public static void GetAllChilds(this ProductCategory parentcate, List<ProductCategory> childs)
        {
            if (parentcate == null)
            {
                foreach (ProductCategory ac in GetAll().Where(ent => ent.ParentID == null).ToList())
                {
                    childs.Add(ac);
                    GetAllChilds(ac, childs);
                }
            }
            else
            {
                foreach (ProductCategory ac in GetAll().Where(ent => ent.ParentID == parentcate.CategoryID).ToList())
                {
                    childs.Add(ac);
                    GetAllChilds(ac, childs);
                }
            }

        }

        public static List<ProductCategory> GetAllChilds(this ProductCategory parentcate)
        {
            List<ProductCategory> listChilds = new List<ProductCategory>();
            parentcate.GetAllChilds(listChilds);
            return listChilds;
        }

        /// <summary>
        /// Lay danh sach nhung nhom cha cua nhom hien tai.
        /// </summary>
        /// <param name="childcate"></param>
        /// <returns></returns>
        public static List<ProductCategory> GetParents(this ProductCategory childcate)
        {
            List<ProductCategory> listParents = new List<ProductCategory>();

            while (childcate.ParentID != null)
            {
                childcate = GetAll().SingleOrDefault(ent => ent.CategoryID == childcate.ParentID);
                if (childcate == null)
                {
                    break;
                }
                listParents.Add(childcate);
            }

            return listParents;
        }


        /// <summary>
        /// Lay ra danh sach nhom con cua nhom hien tai.
        /// </summary>
        /// <param name="parentcate"></param>
        /// <returns></returns>
        public static List<ProductCategory> GetChilds(this ProductCategory parentcate)
        {
            if (parentcate == null)
            {
                return GetAll().Where(ac => ac.ParentID == null).ToList();
            }
            return GetAll().Where(ac => ac.ParentID == parentcate.CategoryID).ToList();
        }

        /// <summary>
        /// Lay ra danh sach tat ca du lieu
        /// </summary>
        /// <returns></returns>
        public static IQueryable<ProductCategory> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = from ac in bitEnt.ProductCategories
                       where ac.IsDeleted == false
                       orderby ac.SortOrder descending, ac.CategoryID descending
                       select ac;
            return list;

        }

        /// <summary>
        /// Lay ra doi tuong theo ID
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static ProductCategory GetByID(int categoryID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.ProductCategories.SingleOrDefault(ac => ac.CategoryID == categoryID);
        }

        /// <summary>
        ///  Lay ra danh sach du lieu theo dieu kien truyen vao
        /// </summary>
        /// <param name="languageCode"></param>
        /// <param name="isEnabled"></param>
        /// <param name="parentID"></param>
        /// <param name="allowUserPost"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="sort"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static List<ProductCategory> GetByCondition(string languageCode = null, bool? isEnabled = null, int? parentID = -1, bool? allowUserPost = null, int start = -1, int limit = -1, string sort = null, string dir = null)
        {
            //ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = GetAll().Where(ac => (languageCode == null || ac.LanguageCode.Equals(languageCode)) &&
                (isEnabled == null || ac.IsEnabled == isEnabled) &&
                (parentID == -1 || ac.ParentID == parentID) &&
                (allowUserPost == null || ac.AllowUserPost == allowUserPost)
                );

            //Start Sorting
            if (sort != null)
            {
                var param = Expression.Parameter(typeof(ProductCategory), "ac");
                var sortExpression = Expression.Lambda<Func<ProductCategory, object>>(Expression.Property(param, sort), param);
                if (dir == "DESC")
                {
                    list = list.OrderByDescending(sortExpression);
                }
                else
                {
                    list = list.OrderBy(sortExpression);
                }
            }

            //Start Paging
            if (start >= 0 && limit > 0)
            {
                list = list.Skip(start).Take(limit);
            }

            return list.ToList();
        }

        /// <summary>
        /// Lay duong link da Rewrite
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        public static string GetSEOUrl(this ProductCategory cat)
        {
            return "/Product/" + cat.Name.ToSeoString() + "-pc" + cat.CategoryID + ".aspx";
        }

    }
}